package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.MchntAcctInfoMapper;
import cc.rengu.igas.bomp.common.dao.MchntSignInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntAcctInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.MchntSignInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntAcctInfo;
import cc.rengu.igas.bomp.common.entity.MchntSignInfo;
import cc.rengu.igas.bomp.common.enums.AliPayRiskTypeEnum;
import cc.rengu.igas.bomp.common.enums.ArtifCertTypeEbnum;
import cc.rengu.igas.bomp.common.enums.LicenseTypeEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.AliPayRiskMchntQueryRequest;
import cc.rengu.igas.bomp.facade.response.AliPayRiskMchntQueryResponse;
import cc.rengu.igas.channel.alipay.facade.request.AliRiskMchntQueryRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliRiskMchntQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.enums.BaseProdEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/03/26  支付宝风险商户查询服务
 */
public class AliPayRiskMchntQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        AliPayRiskMchntQueryRequest aliPayRiskMchntQueryRequest = new AliPayRiskMchntQueryRequest();
        ConvertUtil.convertOutput(aliPayRiskMchntQueryRequest);
        return aliPayRiskMchntQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AliPayRiskMchntQueryRequest aliPayRiskMchntQueryRequest = (AliPayRiskMchntQueryRequest) request;
        AliPayRiskMchntQueryResponse aliPayRiskMchntQueryResponse = new AliPayRiskMchntQueryResponse();
        /* 获取法人行标识 */
        String instId = aliPayRiskMchntQueryRequest.getHeader().getInstId();
        /* 获取风险类型 */
        String riskType = aliPayRiskMchntQueryRequest.getRiskType();
        /*获取用户标识 */
        String userId = aliPayRiskMchntQueryRequest.getUserId();
        /*获取商户号信息 */
        String mchntNo = aliPayRiskMchntQueryRequest.getMchntNo();
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(riskType) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*dubbo请求初始化*/
        AliRiskMchntQueryRequest aliRiskMchntQueryRequest = new AliRiskMchntQueryRequest();
        //获取通道标识,设置通道标识（需要修改，通道标识和通道序号，这个是从交易码映射表中获取）
        List<ChannelCallCfg> channelCallCfgList = txnInfo.getChannelCallCfgList();
        List<ChannelCallCfg> msgDstchannelCallCfgList = channelCallCfgList.stream().filter(item -> item.getCallChannelId().equals("UPWX") || item.getCallChannelId().equals("NUWX") || item.getCallChannelId().equals("UPAL") || item.getCallChannelId().equals("NUAL") || item.getCallChannelId().equals("UPQC")).collect(Collectors.toList());
        String channelId = msgDstchannelCallCfgList.get(0).getCallChannelId();
        /*通道标识赋值*/
        aliRiskMchntQueryRequest.setChannelId(channelId);
        /* 银行卡风险查询 */
        if (riskType.contains(AliPayRiskTypeEnum.BANK_CARD.getRiskType())) {
            /* 查询结算账户信息 */
            MchntAcctInfoMapper mchntAcctInfoMapper = new MchntAcctInfoMapperImpl();
            MchntAcctInfo mchntAcctInfo = mchntAcctInfoMapper.selectMchntAcctInfoByPrimaryKey(instId, mchntNo, BaseProdEnum.ALIPAY_PAY.getProdCode(), "00");
            if (null != mchntAcctInfo) {
                aliRiskMchntQueryRequest.setSettleAcctNo(mchntAcctInfo.getSettleAcctNo());
            } else {
                rglog.error("结算账户信息为空!");
                throw new BizException(RespCodeEnum.QUERY_ACCT_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_ACCT_INFO_ERROR.getRespDesc());
            }
        }
        /* 身份证风险 和 营业执照风险  查询*/
        if (riskType.contains(AliPayRiskTypeEnum.CERT_NO.getRiskType()) || riskType.contains(AliPayRiskTypeEnum.BUSINESS_LICENSE.getRiskType())) {
            /* 查询商户签约信息 */
            MchntSignInfoMapper MchntSignInfoMapper = new MchntSignInfoMapperImpl();
            MchntSignInfo mchntSignInfo = MchntSignInfoMapper.selectMchntSignInfoByPrimaryKey(instId, mchntNo);
            if (null != mchntSignInfo) {
                /* 判断证件类型是身份证 */
                if (riskType.contains(AliPayRiskTypeEnum.CERT_NO.getRiskType())) {
                    if (ArtifCertTypeEbnum.ID_CARD_CERT.getCardCode().equals(mchntSignInfo.getArtifCertType())) {
                        aliRiskMchntQueryRequest.setCertNo(mchntSignInfo.getArtifCertNo());
                    } else {
                        rglog.error("商户法人证件类型非身份证!");
                        throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                    }
                }
                /*营业执照风险查询*/
                if (riskType.contains(AliPayRiskTypeEnum.BUSINESS_LICENSE.getRiskType())) {
                    if (LicenseTypeEnum.BUSINESS_LICENSE.getLicenseType().equals(mchntSignInfo.getLicenceType())) {
                        /*营业执照号赋值 */
                        aliRiskMchntQueryRequest.setLicenseNo(mchntSignInfo.getLicenceCode());
                        /* 营业执照名称赋值 */
                        aliRiskMchntQueryRequest.setCompanyName(mchntSignInfo.getLicenceName());
                    } else {
                        rglog.error("经营执照类型非营业执照!");
                        throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
                    }
                }
                /* 法人手机号 */
                aliRiskMchntQueryRequest.setMobileNo(mchntSignInfo.getArtifMobile());
            } else {
                rglog.error("商户签约信息为空!");
                throw new BizException(RespCodeEnum.QUERY_MCHNT_SIGN_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_MCHNT_SIGN_INFO_ERROR.getRespDesc());
            }
        }

        BizResponse<AliPayRiskMchntQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        /* 通过dubbo服务调用通道方法查询支付宝通道商户风险信息 */
        DubboService dubboService = new DubboServiceImpl();
        AliRiskMchntQueryResponse aliRiskMchntQueryResponse = (AliRiskMchntQueryResponse) dubboService.callDubboService("AlipayMchntService", "aliRiskQuery", aliRiskMchntQueryRequest);
        if (null == aliRiskMchntQueryResponse) {
            rglog.error("调dobbu失败，respCode={},respDesc={}", OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setResult(aliPayRiskMchntQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            return bizResponse;
        }
        aliPayRiskMchntQueryResponse.setRiskResult(aliRiskMchntQueryResponse.getRiskResult());
        aliPayRiskMchntQueryResponse.setRiskResultDesc(aliRiskMchntQueryResponse.getRiskResultDesc());
        bizResponse.setResult(aliPayRiskMchntQueryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AliPayRiskMchntQueryResponse aliPayRiskMchntQueryResponse = (AliPayRiskMchntQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(aliPayRiskMchntQueryResponse);
    }
}
